package com.dong.web.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.collections.map.CaseInsensitiveMap;

import com.dong.web.whold.Variables;

@SuppressWarnings("unchecked")
public class ConfigUtil {

	public static Map<String, String> getConfig() {
		Map<String, String> rn = new CaseInsensitiveMap();
		String path = ClassUtil.getPath() + Variables.CONFIG_NAME;
		if (new File(path).exists()) {
			InputStream input = ConfigUtil.class.getClassLoader()
					.getResourceAsStream(Variables.CONFIG_NAME);
			if (input != null) {
				try {
					Properties properties = new Properties();
					properties.load(input);
					for (Map.Entry<Object, Object> entry : properties
							.entrySet()) {
						rn.put(entry.getKey().toString(), entry.getValue()
								.toString());
					}
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (input != null) {
						try {
							input.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return rn;
	}

	public static List<String> dom4jUtil() {
		org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
		List<String> list = new ArrayList<String>();
		try {
			org.dom4j.Document document = reader.read(new File(ClassUtil
					.getPath() + Variables.C3P0_NAME));
			org.dom4j.Element root = document.getRootElement();
			List<org.dom4j.Node> nodeList = root.selectNodes("named-config");
			for (org.dom4j.Node node : nodeList) {
				org.dom4j.Element element = (org.dom4j.Element) node;
				list.add(element.attributeValue("name"));
			}
		} catch (org.dom4j.DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}

}
